//
//  BianKuangXingZhuangViewController.swift
//  ZHM
//
//  Created by zzddongge on 16/9/13.
//  Copyright © 2016年 zzd. All rights reserved.
//

import UIKit

protocol SheZhiLabelXingZhuangDelegate:class {
    func chuanDiXuanZhongFenZhongZhiAndXingZhuang(_ xuanZhongDeFenZhongZhi:Int,xuanZhongXingZhuangByIndexPathDianRow:Int)
}

class BianKuangXingZhuangViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    //定义代理变量
    weak var delegate:SheZhiLabelXingZhuangDelegate?
    
    let screenWidth = UIScreen.main.bounds.width
    let screenHeight = UIScreen.main.bounds.height
    
    var navigationHeight:CGFloat?
    
    //定义UITableView
    var sheZhiBianKuangXingZhuang:UITableView?
    
    //完成按钮
    var wanChengBtn:UIButton?
    
    //定义两个变量,用于判断该tableView是否被选中
    var row0Checked:Bool = false
    var row1Checked:Bool = false
    
    var indexPathDianRow:Int = 16
    
    var selectedMiniteValue:Int = 0

    override func viewDidLoad() {
        
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
        //修改导航栏按钮颜色
        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.navigationController?.navigationBar.barTintColor =
            UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1)
        
        let leftBarBtn = UIBarButtonItem(title: "取消", style: .plain, target: self,action: #selector(backToPrevious))
        self.navigationItem.leftBarButtonItem = leftBarBtn
        self.view.backgroundColor = UIColor.white
        self.title = "形状设置"
        //获取导航栏的高度
        navigationHeight = self.navigationController?.navigationBar.layer.bounds.height
        
        //初始化右侧的按钮
        wanChengBtn = UIButton(frame:CGRect(x: 0, y: 0, width: 60, height: 30))
        wanChengBtn?.setTitle("完成", for: UIControlState())
        wanChengBtn?.setTitleColor(UIColor.white, for: UIControlState())
       wanChengBtn!.addTarget(self,action:#selector(xiuGaiLabelXingZhuang),for:.touchUpInside)
        let rightBarButtonItem = UIBarButtonItem(customView: wanChengBtn!)
        self.navigationItem.rightBarButtonItem = rightBarButtonItem
     
        //初始化UITableView
        sheZhiBianKuangXingZhuang = UITableView(frame: CGRect(x: 0, y: navigationHeight!+20, width: screenWidth, height: screenHeight))
        //设置代理和数据源
        self.sheZhiBianKuangXingZhuang?.delegate = self
        self.sheZhiBianKuangXingZhuang?.dataSource = self
        self.view.addSubview(wanChengBtn!)
        self.view.addSubview(sheZhiBianKuangXingZhuang!)
        
        print(selectedMiniteValue)//2
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    /*
     作者:张振东
     时间:2016-9-18
     功能:返回几组
     */
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    
    /*
     作者:张振东
     时间:2016-9-18
     功能:每组显示几个cell
     */
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

    /*
     作者:张振东
     时间:2016-9-18
     功能:定义tableViewcell的高度
     */
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }

    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "cellIdentifier"
        var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as UITableViewCell?
        
        if cell == nil{
            cell = UITableViewCell(style: UITableViewCellStyle.default,reuseIdentifier: cellIdentifier)
            cell!.textLabel?.font = UIFont.systemFont(ofSize: 12)
            cell!.selectionStyle = .gray
            cell!.accessoryType = .none
            tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        configureCheckmarkForCell(cell!, indexPath: indexPath)
        //tableViewCell分割线从头开始显示
        tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        switch indexPath.row {
        case 0:
            cell!.textLabel?.text = "正方形"
            cell?.imageView?.image = UIImage(named:"quweixunlian")
            
        case 1:
            
            cell?.imageView?.image = UIImage(named:"quweixunlian")
            cell!.textLabel?.text = "圆形"
        default:
            
            print("--")
        }
        return cell!
    }

    /*
        作者:张振东
        时间:2016-9-19
        功能:点击每个cell时触发
     */
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        indexPathDianRow = indexPath.row
        print(indexPathDianRow)
        if let cell = tableView.cellForRow(at: indexPath){
            var isChecked = false
            if indexPath.row == 0{
                row0Checked = !row0Checked
                isChecked = row0Checked
            }else if indexPath.row == 1{
                row1Checked = !row1Checked
                isChecked = row1Checked
            }
            if isChecked{
                cell.accessoryType = .checkmark
            }else{
                cell.accessoryType = .none
                
            }
        }
    }
    
    
    func configureCheckmarkForCell(_ cell:UITableViewCell,indexPath:IndexPath){
        var isChecked = false
        if indexPath.row == 0{
            isChecked = row0Checked
        }else if indexPath.row == 1{
            isChecked = row1Checked
        }
        if isChecked{
            cell.accessoryType = .checkmark
        }else{
            cell.accessoryType = .none
        }
    }
    
    /*
        作者:张振东
        时间:2016-9-19
        功能:修改label的形状
     */
    func xiuGaiLabelXingZhuang(){
        print(indexPathDianRow)
        switch indexPathDianRow {
        case 0:
            print("0")
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndXingZhuang(selectedMiniteValue, xuanZhongXingZhuangByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
            
        case 1:
            print("1")
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndXingZhuang(selectedMiniteValue, xuanZhongXingZhuangByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)
        default:
            print("default")
            print(selectedMiniteValue)
            //接下来要用协议传递值,传递到上一个viewController
            delegate?.chuanDiXuanZhongFenZhongZhiAndXingZhuang(selectedMiniteValue, xuanZhongXingZhuangByIndexPathDianRow: indexPathDianRow)
            print(self.navigationController?.viewControllers as Any)
            self.navigationController?.popToViewController((self.navigationController?.viewControllers[3])!, animated: false)

        }
    }
    
    //返回至上一个界面
    func backToPrevious(){
        self.navigationController?.popViewController(animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
